package cn.ekuma.epos.panel;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.ListCellRenderer;
import javax.swing.tree.TreeCellRenderer;

import com.openbravo.pos.base.AppLocal;
import e.odbo.data.dao.I_DataLogic;
import e.odbo.data.bean.I_Category;
import cn.ekuma.data.dao.user.BrowsableEditableDataModel;
import cn.ekuma.data.dao.user.tree.BrowsableEditableTreeDataModel;
import cn.ekuma.data.ui.swing.SwingEditorRecord;

import com.openbravo.data.basic.BasicException;
import com.openbravo.data.gui.JCounter;
import com.openbravo.data.gui.JLabelDirty;
import com.openbravo.data.gui.JListNavigator;
import com.openbravo.data.gui.JNavigator;
import com.openbravo.data.gui.JSaver;
import com.openbravo.data.gui.JTreeNavigator;
import com.openbravo.data.gui.ListCellRendererBasic;
import com.openbravo.data.gui.MessageInf;
import com.openbravo.data.gui.TreeCellRendererBasic;
import com.openbravo.data.loader.ComparatorCreator;
import com.openbravo.data.loader.IRenderString;
import com.openbravo.data.loader.Vectorer;
import com.openbravo.data.user.DirtyManager;
import com.openbravo.data.user.EditorCreator;
import com.openbravo.data.user.I_SwingBrowsableEditableData;

import com.openbravo.pos.base.BeanFactoryApp;
import com.openbravo.pos.base.BeanFactoryException;
import com.openbravo.pos.base.I_AppView;
import com.openbravo.pos.forms.JPanelView;

public abstract class AbstractJPanelTableDAO<T> extends javax.swing.JPanel implements JPanelView, BeanFactoryApp{

    protected I_SwingBrowsableEditableData bd;
    protected DirtyManager dirty;
    protected I_AppView app;
   
    /** Creates new form JPanelTableDAO */
    public AbstractJPanelTableDAO() {
        initComponents();
    }

     public void init(I_AppView app) throws BeanFactoryException {
        this.app =  app;
        dirty = new DirtyManager();
        bd = null;
        init();
    }

    public Object getBean() {
        return this;
    }

    protected void startNavigation() {
        if (bd == null) {
            // Add the filter panel
            Component c = getFilter();
            if (c != null) {
                c.applyComponentOrientation(getComponentOrientation());
                add(c, BorderLayout.NORTH);
            }

            Class suportClass=getSuportClass();
            
            if(I_Category.class.isAssignableFrom(suportClass)){
            	 // init browsable editable data
                bd = new BrowsableEditableTreeDataModel(getDataLogic(),getSuportClass(), getEditor(), dirty,(EditorCreator)c);

                // el panel este
                TreeCellRenderer cr = getTreeCellRenderer();
                if (cr != null) {
                    JTreeNavigator nl = new JTreeNavigator((BrowsableEditableTreeDataModel) bd);
                    nl.applyComponentOrientation(getComponentOrientation());
                    if (cr != null) nl.setCellRenderer(cr);
                    container.add(nl, java.awt.BorderLayout.LINE_START);
                }
            }else{
            
               // init browsable editable data
                bd = new BrowsableEditableDataModel(getDataLogic(),getSuportClass(), getEditor(), dirty,(EditorCreator)c);

                // el panel este
                 ListCellRenderer cr = getListCellRenderer();
                if (cr != null) {
                    JListNavigator nl = new JListNavigator((BrowsableEditableDataModel) bd);
                    nl.applyComponentOrientation(getComponentOrientation());
                    if (cr != null) nl.setCellRenderer(cr);
                    container.add(nl, java.awt.BorderLayout.LINE_START);
                }
            }

           // Add the editor
            c = getEditor().getComponent();
            if (c != null) {
                c.applyComponentOrientation(getComponentOrientation());
                container.add(c, BorderLayout.CENTER);
            }
            
            
            // add toolbar extras
            c = getToolbarExtras();
            if (c != null) {
                c.applyComponentOrientation(getComponentOrientation());
                toolbar.add(c);
            }

            // La Toolbar
         // La Toolbar
            
            JButton but = new javax.swing.JButton();
            but.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/openbravo/images/2rightarrow.png")));
            but.applyComponentOrientation(getComponentOrientation());
            toolbar.add(but);
            but.addActionListener(new java.awt.event.ActionListener(){

				@Override
				public void actionPerformed(ActionEvent e) {
					getFilter().setVisible(!getFilter().isVisible());
				}
            	
            });
            but.setEnabled(getFilter()!=null);
            
            c = new JLabelDirty(dirty);
            c.applyComponentOrientation(getComponentOrientation());
            toolbar.add(c);
            c = new JCounter(bd);
            c.applyComponentOrientation(getComponentOrientation());
            toolbar.add(c);
            c = new JNavigator(bd, getVectorer(), getComparatorCreator());
            c.applyComponentOrientation(getComponentOrientation());
            toolbar.add(c);
            c = new JSaver(bd);
            c.applyComponentOrientation(getComponentOrientation());
            toolbar.add(c);
        }
    }

    public Component getToolbarExtras() {
        return null;
    }

    public abstract Component getFilter();

    protected abstract void init();

    public abstract SwingEditorRecord getEditor();

    public abstract I_DataLogic getDataLogic ();

    public abstract Class getSuportClass();

    public Vectorer getVectorer() {
        return null;
    }

    public ComparatorCreator getComparatorCreator() {
        return null;
    }

    public ListCellRenderer getListCellRenderer() {
        return new ListCellRendererBasic(new IRenderString<T>(){
            public String getRenderString(T value) {
               return  getObjectRenderString(value);
            }
        });
    }

    public TreeCellRenderer getTreeCellRenderer() {
        return new TreeCellRendererBasic(new IRenderString<T>(){
            public String getRenderString(T value) {
               return getObjectRenderString(value);
            }
        });
    }
    
    public abstract String getObjectRenderString(T obj);

    public JComponent getComponent() {
        return this;
    }

    public void activate() throws BasicException {
        getEditor().activate();
        startNavigation();
        bd.actionLoad();
    }

    public boolean deactivate() {
        try {
            return bd.actionClosingForm(this);
        } catch (BasicException eD) {
            MessageInf msg = new MessageInf(MessageInf.SGN_NOTICE, AppLocal.getIntString("message.CannotMove"), eD);
            msg.show(this);
            return false;
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        container = new javax.swing.JPanel();
        toolbar = new javax.swing.JPanel();

        setLayout(new java.awt.BorderLayout());

        container.setLayout(new java.awt.BorderLayout());
        container.add(toolbar, java.awt.BorderLayout.NORTH);

        add(container, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel container;
    private javax.swing.JPanel toolbar;
    // End of variables declaration//GEN-END:variables

}
